package cn.cyber.array;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// 用户数组
		String[] students = {"唐僧","孙悟空","猪八戒","沙僧"};
		// 数组长度
		int length = 0;
		// 元素是否在数组中（默认false）
		boolean isInArray = false;
		// 用户选择
        int choice = 0;
        Scanner scanner = new Scanner(System.in);
		do {
			System.out.println("*****************************************************");
            System.out.println("************* Please Choose Your Action: ************");
            System.out.println("**** 1.show all     2.add user     3.delete user ****");
            System.out.println("**** 4.lock user    5.unlock user  6.exit        ****");
            System.out.println("*****************************************************");
            choice = scanner.nextInt();
            switch (choice) {
			case 1:
				System.out.println("编号\t名字");
				for (int i = 0; i < students.length; i++) {
					System.out.print((i+1) + "\t");
					System.out.print(students[i]);
					System.out.println();
				}
				break;
			case 2:
				System.out.println("Please input the user's name：");
				String name = scanner.next();
				
				isInArray = false;
				// 判断要添加的用户名是否在数组中
                for (String nameTmp : students) {
                	// 如果在数组中
                    if (name.equals(nameTmp)) {
                    	isInArray = true;
                        System.out.println("The user you want to add is already in system!");
                        break;
                    }
                }
                // 如果不在数组中,则对数组进行扩容
                if (!isInArray) {
                	length = students.length;
                    students = Arrays.copyOf(students, length+1);
                    students[length] = name;
				}
				break;
			case 3:
				System.out.println("You choose \"delete user\"!");
				break;
			case 4:
				System.out.println("You choose \"lock user\"!");
				break;
			case 5:
				System.out.println("You choose \"unlock user\"!");
				break;
			case 6:
				System.out.println("You choose \"exit\"!Bye Bye!");
				break;
			default:
				System.out.println("Please input 1~6!");
				break;
			}
		} while (choice != 6);
		scanner.close();
	}
}
